// 字符 || 数字类型
const rules = {
  qq: /^\d{5,16}$/,
  number: /^[0-9]+$/,
  mobile: /^(1|9)\d{10}$/,
  amount: /^\d+(\.\d{1,2})?$/,
  name: /^[a-zA-Z\u4E00-\u9FA5·\u00B7]+$/,
  password: /[a-zA-Z\d`~!@#$%^&*()_\-+={}[\]\\|:;"'<>,.?/]{6,16}/,
  email: /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/
}
/**
 * 正则匹配
 * @param {RegExp, v}
 * @return {Boolean}
 */
const regTest = (reg, v) => reg.test(v)
export const isQQ = (v) => regTest(rules.qq, v)
export const isName = (v) => regTest(rules.name, v)
export const isEmail = (v) => regTest(rules.email, v)
export const isMobile = (v) => regTest(rules.mobile, v)
export const isNumber = (v) => regTest(rules.number, v)
export const isAmount = (v) => regTest(rules.amount, v)
export const isPassword = (v) => regTest(rules.password, v)
/**
 * 身份证校验
 * @param {String} 身份证号码
 * @returns {Boolean}
 */
export const isCreditid = (v) => {
  const arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  const arrValid = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] // 校验码
  let y, m, d
  const funIsValidDate = function (y, m, d) {
    const now = new Date(y, m - 1, d, 12, 0, 0)
    const _y = now.getFullYear()
    const _m = now.getMonth() + 1
    const _d = now.getDate()
    return (+y === _y && +m === _m && +d === _d)
  }
  if (/^\d{15}$/.test(v)) {
    y = '19' + v.substr(6, 2)
    m = v.substr(8, 2)
    d = v.substr(10, 2)
    return funIsValidDate(y, m, d)
  } else if (/^\d{17}\d|x$/i.test(v)) {
    let sum = 0
    for (let i = 0; i < v.length - 1; i++) {
      sum += parseInt(v.substr(i, 1), 10) * arrExp[i]
    }
    const vBit = sum % 11
    y = v.substr(6, 4)
    m = v.substr(10, 2)
    d = v.substr(12, 2)
    return (arrValid[vBit] === v.substr(17, 1).toUpperCase() && funIsValidDate(y, m, d))
  } else {
    return false
  }
}

// 类型判断
export const isObject = (obj) => {
  return Object.prototype.toString.call(obj) === '[object Object]'
}
export const isFunction = (obj) => {
  return Object.prototype.toString.call(obj) === '[object Function]'
}

// 语言类型
export const isKorean = (text) => {
  const reg = /([(\uAC00-\uD7AF)|(\u3130-\u318F)])+/gi
  return reg.test(text)
}

// 文件类型
export const isImage = (name = '') => {
  const arr = name.split('.')
  const houzui = arr.length > 1 ? arr[arr.length - 1] : ''
  if (name && houzui) {
    const arr = ['png', 'jpg', 'bmp', 'wbmp', 'jpeg', 'webp', 'mbm']
    return ~arr.indexOf(houzui.toLowerCase())
  }
  return false
}
export const isVideo = (name = '') => {
  const arr = name.split('.')
  const houzui = arr.length > 1 ? arr[arr.length - 1] : ''
  if (name && houzui) {
    const arr = ['mp4', '3gp', 'avi', 'mov', 'wmv', 'rmvb', 'mkv']
    return ~arr.indexOf(houzui.toLowerCase())
  }
  return false
}
